package cn.edu.sicnu.cardgame.ui

import android.os.Bundle
import android.net.Uri
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.activity.result.contract.ActivityResultContracts
import cn.edu.sicnu.cardgame.R
import cn.edu.sicnu.cardgame.data.ProfileRepository

class EditInfoActivity : AppCompatActivity() {
    private var avatarUri: String = ""

    private val pickImage = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
        if (uri != null) {
            avatarUri = uri.toString()
            findViewById<ImageView>(R.id.ivAvatarPreview).setImageURI(uri)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_info)

        val etName: EditText = findViewById(R.id.etName)
        val etStudentId: EditText = findViewById(R.id.etStudentId)
        val etPhone: EditText = findViewById(R.id.etPhone)
        val etEmail: EditText = findViewById(R.id.etEmail)
        val rgGender: RadioGroup = findViewById(R.id.rgGender)
        val rbMale: RadioButton = findViewById(R.id.rbMale)
        val rbFemale: RadioButton = findViewById(R.id.rbFemale)
        val ivAvatar: ImageView = findViewById(R.id.ivAvatarPreview)

        // preload existing values
        val p = ProfileRepository.profile
        etName.setText(p.name)
        etStudentId.setText(p.studentId)
        etPhone.setText(p.phone)
        etEmail.setText(p.email)
        avatarUri = p.avatarUri
        if (avatarUri.isNotEmpty()) ivAvatar.setImageURI(Uri.parse(avatarUri))
        when (p.gender) {
            getString(R.string.gender_male) -> rbMale.isChecked = true
            getString(R.string.gender_female) -> rbFemale.isChecked = true
        }

        findViewById<Button>(R.id.btnPickAvatar).setOnClickListener {
            pickImage.launch("image/*")
        }

        findViewById<Button>(R.id.btnSave).setOnClickListener {
            val name = etName.text?.toString()?.trim().orEmpty()
            val studentId = etStudentId.text?.toString()?.trim().orEmpty()
            val phone = etPhone.text?.toString()?.trim().orEmpty()
            val email = etEmail.text?.toString()?.trim().orEmpty()
            val gender = when (rgGender.checkedRadioButtonId) {
                R.id.rbMale -> getString(R.string.gender_male)
                R.id.rbFemale -> getString(R.string.gender_female)
                else -> ""
            }
            // hobbies from chips
            val chipIds = listOf(R.id.chipTravel, R.id.chipReading, R.id.chipGaming, R.id.chipShopping)
            val hobbies = chipIds.mapNotNull { id ->
                val chip = findViewById<com.google.android.material.chip.Chip>(id)
                if (chip.isChecked) chip.text.toString() else null
            }.joinToString(separator = " ")

            ProfileRepository.update(name, gender, studentId, phone, email, hobbies, avatarUri)
            setResult(RESULT_OK)
            finish()
        }

        findViewById<Button>(R.id.btnCancel).setOnClickListener { finish() }
    }
}

